package com.zkadmin.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

@Entity
@Table(name = "sys_user")
public class SysUser extends AuditorAndVersionEntity {
	private static final long serialVersionUID = -8965004854780703859L;
	@Column(name = "username", unique = true, nullable = false, length = 20)
	private String username;
	@Column(unique = true, nullable = false, length = 50)
	private String name;

	@Column(nullable = false, length = 50)
	private String password;
	@Column(name = "password_salt", nullable = false)
	private String passwordSalt;
	@Column(length = 18)
	private String mobile;
	@Column(length = 30)
	private String email;
	private String descn;
	@Column(name = "locked")
	private Boolean locked = false;
	@Column(name = "logined_at")
	private Date lastLoginedAt;

	@ManyToMany(cascade = CascadeType.REFRESH)
	@JoinTable(name = "sys_user_role", inverseJoinColumns = @JoinColumn(name = "role_id"), joinColumns = @JoinColumn(name = "user_id"))
	private Set<SysRole> roles = new HashSet<SysRole>();

	public String getCredentialsSalt() {
		return username + passwordSalt;
	}

	public void addRole(SysRole role) {
		if (role != null) {
			roles.add(role);
		}
	}

	public void removeRole(SysRole role) {
		if (role != null && roles.contains(role)) {
			roles.remove(role);
		}
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordSalt() {
		return passwordSalt;
	}

	public void setPasswordSalt(String passwordSalt) {
		this.passwordSalt = passwordSalt;
	}

	public String getDescn() {
		return descn;
	}

	public void setDescn(String descn) {
		this.descn = descn;
	}

	public Boolean getLocked() {
		return locked;
	}

	public void setLocked(Boolean locked) {
		this.locked = locked;
	}

	public Date getLastLoginedAt() {
		return lastLoginedAt;
	}

	public void setLastLoginedAt(Date lastLoginedAt) {
		this.lastLoginedAt = lastLoginedAt;
	}

	public Set<SysRole> getRoles() {
		return roles;
	}

	public void setRoles(Set<SysRole> roles) {
		this.roles = roles;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

}
